package com.example.tse.rbhttpservice.service.member;


import android.content.Context;

import com.example.tse.rbhttpservice.service.HttpService;
import com.example.tse.rbhttpservice.service.RemoteServiceListenerT;
import com.example.tse.rbhttpservice.service.ResponseServiceListenerT;
import com.example.tse.rbhttpservice.service.RudderResponse;
import com.example.tse.rbhttpservice.service.member.model.MemberGroupGetList;
import com.example.tse.rbhttpservice.service.member.model.MemberGroupGetListRequest;
import com.example.tse.rbhttpservice.service.member.model.MemberGroupGetListResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.kymjs.kjframe.http.HttpParams;

import java.lang.reflect.Type;
import java.util.List;


/**
 * 
 * @author wliu
 *    edi  tse
 */
public class MemberGroupServiceImpl implements MemberGroupService {
	private Context context;
	private static Gson gson = new Gson();
	
	
	public MemberGroupServiceImpl(Context context) {
		this.context = context;
	}
	/**
	 * 获取我的团购列表，分享功能同团购详情中
	 * @param request
	 * @param listener
	 * @return MemberGroupGetListResponse
	 */		
	 
	    public void getList(MemberGroupGetListRequest request, final RemoteServiceListenerT<MemberGroupGetListResponse> listener) {

        final HttpParams params = new HttpParams();
        


        final String url = "member/group/getList";


        
        /**
        group
        getList
        GetList
        获取我的团购列表，分享功能同团购详情中
        GET
        MemberGroup
        MemberGroupGetList
        member/group/getList
        array
        */
	
           final Type type = new TypeToken<List<MemberGroupGetList>>() {
        }.getType();



            new HttpService().getList(context, url, params, MemberGroupGetListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

             @Override
             public void onPreStart() {
                 super.onPreStart();
                 listener.PreStart();
             }

             @Override
             public void ok(RudderResponse rudderResponse) {
                 super.ok(rudderResponse);
                 List<MemberGroupGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                 MemberGroupGetListResponse responce = new MemberGroupGetListResponse();
                 responce.setGetList(items);
                 listener.ok(responce);
             }

             @Override
             public void okData(RudderResponse rudderResponse) {
                 super.okData(rudderResponse);

                 if (rudderResponse == null) {
                     listener.okData(new MemberGroupGetListResponse());
                 } else {
                     List<MemberGroupGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MemberGroupGetListResponse responce = new MemberGroupGetListResponse();
                     responce.setGetList(items);
                     listener.okData(rudderResponse == null ? new MemberGroupGetListResponse() : responce);
                 }
             }

             @Override
             public void okData(RudderResponse rudderResponse, int fromWhere) {
                 super.okData(rudderResponse, fromWhere);


                 if (rudderResponse == null) {
                     listener.okData(new MemberGroupGetListResponse(), fromWhere);
                 } else {
                     List<MemberGroupGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MemberGroupGetListResponse responce = new MemberGroupGetListResponse();
                     responce.setGetList(items);
                     listener.okData(rudderResponse == null ? new MemberGroupGetListResponse() : responce, fromWhere);
                 }


             }

             @Override
             public void onFailure(int errorNo, String strMsg) {
                 super.onFailure(errorNo, strMsg);
                 listener.failue(errorNo, strMsg);
             }

             @Override
             public void onFinish() {
                 super.onFinish();
                 listener.Finish();
             }
         });
    }            
        
        

   
	
	
}